//
//  M1ShopDetailViewController.m
//  NewMarryU
//
//  Created by apple on 2018/8/1.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1ShopDetailViewController.h"

@interface M1ShopDetailViewController ()<UIWebViewDelegate>
@property (nonatomic,strong) UIWebView *m1_webView;

@end

@implementation M1ShopDetailViewController
-(void)dealloc{
    _m1_webView.delegate = nil;
    _m1_webView =nil;
    [_m1_webView removeFromSuperview];
}
-(void)m1_back{
    if ([_m1_webView canGoBack]) {
        [_m1_webView goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    _m1_webView = [[UIWebView alloc]init];
    _m1_webView.delegate = self;
    [self.view addSubview:_m1_webView];
    
    NSString *urlString = _m1_url;
    NSString* encodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [_m1_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:encodedString]]];
    if (@available(iOS 11.0, *))
    {
        _m1_webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
        
    }
    [self.view addSubview:_m1_webView];
    [_m1_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(m1_NAV_HEIGHT, 0, 0, 0));
    }];
    
    [_m1_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_m1_url]]];
    
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    if ([request.URL.absoluteString rangeOfString:@"orderDetails?id"].location != NSNotFound) {
        
        if (![M1User user]) {
            M1LoginVC *loginVC = [[M1LoginVC alloc] init];
            loginVC.m1_changeNavTitleColor = YES;
            loginVC.m1_changeBackImage = YES;
            loginVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:loginVC animated:YES];
            
            return NO;
        }
    }
    return YES;
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
    [UIApplication sharedApplication].networkActivityIndicatorVisible =NO;
    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
